package recursion;

/**
 * 递归-迷宫问题
 */
public class R {
    public static void main(String[] args) {
        int[][] map=new int[8][7];
        for (int i=0;i<7;i++){
            map[0][i]=1;
            map[7][i]=1;
        }
        for (int i=0;i<8;i++){
            map[i][0]=1;
            map[i][6]=1;
        }
        map[2][1]=1;
        map[6][4]=1;
        for (int[] row:map){
            System.out.println("\n");
            for(int data:row){
                System.out.printf("%d\t", data);
            }
        }
        findWay(map,1,1);
        System.out.println("--------------------");
        for (int[] row:map){
            System.out.println("\n");
            for(int data:row){
                System.out.printf("%d\t", data);
            }
        }
    }
    public static boolean findWay(int[][] map,int i,int j){
        if (map[6][1]==99){
            return true;
        }else {
            if (map[i][j]==0){
                map[i][j]=99;
                for (int[] row:map){
                    System.out.println("\n");
                    for(int data:row){
                        System.out.printf("%d\t", data);
                    }
                }
                System.out.println("\n");
                if (findWay(map,i+1,j)){
                    return true;
                } else if (findWay(map,i,j+1)){
                    return true;
                }else if (findWay(map,i-1,j)){
                    return true;
                }else if (findWay(map,i,j-1)){
                    return true;
                }else {
                    map[i][j]=3;
                    return false;
                }
            } else {
                return false;
            }
        }

    }
}

